import java.rmi.*;
import java.rmi.server.*;
import java.net.*;

//this is the import for the line that
//avoids the command line "start rmiregistry 6666" in the console
import java.rmi.registry.LocateRegistry;

public class RMIServer extends UnicastRemoteObject implements RMI {

	public RMIServer() throws RemoteException {
		super();
	}

	public String sayHello() throws RemoteException {
                System.out.println();
		System.out.println("Printing on server...");
		return "ACK";
	}

	public void remote_print(String s) throws RemoteException {
		System.out.println("Server:" + s);
	}

	public void remote_print(Idea m) throws RemoteException {
		System.out.println("Server:" + m);
	}

        public void publish(Idea m) throws RemoteException {
		System.out.println("Server:" + m.toString());
	}

	public Idea ping_pong(Idea m, User u) throws RemoteException {
                Idea m1 = new Idea(m.getValue(), m.getName().concat("...."), m.getDescription(), u);
		return m1;
	}

	// =======================================================

	public static void main(String args[]) {

		try {
			RMIServer r = new RMIServer();

                        //this avoids the command line "start rmiregistry 6666" in the console
                        LocateRegistry.createRegistry(7007);
                        //pay attention to the name you LOOKUP for (the same as in naming.rebind...)
                        Naming.rebind("rmi://localhost:7007/idea", r);
			//Naming.rebind("hello", h);

			System.out.println("RMI Server ready.");
		} catch (RemoteException re) {
			System.out.println("Exception in RMIServer.main: " + re);
		} catch (MalformedURLException e) {
			System.out.println("MalformedURLException in RMIServer.main: " + e);
		}

	}

}
